﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 小组五日均值
    /// </summary>
    public class GroupFiveDayAvg
    {
        public int Id { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public int GroupId { get; set; }

        public virtual Sys.Groups Group { get; set; }

        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        public void InitGroupFiveDayAvg(int tradeHistoryId, DB.DBManager db)
        {
            List<BenefitHelper.Sys.Groups> groups = db.Groups.ToList();
            foreach (BenefitHelper.Sys.Groups op in groups)
            {
                SetGroupFiveDayAvg(op.Id, tradeHistoryId, db);
            }
            db.SaveChanges();
        }

        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.GroupFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            foreach (BenefitHelper.View.GroupFiveDayAvg avg in query)
            {
                db.GroupFiveDayAvg.Remove(avg);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public GroupFiveDayAvg GetGroupFiveDayAvg(int groupId, int tradeHistoryId, DB.DBManager db)
        {
            return db.GroupFiveDayAvg.Where(a => a.GroupId == groupId).Where(a => a.TradeHistoryId == tradeHistoryId).ToList().First();
        }


        /// <summary>
        /// 初始化交易员五日均值(这个方法应该没问题)
        /// </summary>
        /// <returns></returns>
        public void SetGroupFiveDayAvg(int groupId, int tradeHistoryId, DB.DBManager db)
        {
            GroupFiveDayAvg change = new GroupFiveDayAvg();
            change.GroupId = groupId;
            change.TradeHistoryId = tradeHistoryId;
            Sys.TradeHistory th = new Sys.TradeHistory();
            th = th.GetModel(tradeHistoryId, db);
            List<Sys.Operator> operators = new Sys.DayGroupOperators().GetGroupOperators(tradeHistoryId, Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate), db);
            if (operators.Count > 0)
            {
                foreach (Sys.Operator acc in operators)
                {
                    var queryChange = db.OperatorFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.OperatorId == acc.Id);
                    if (queryChange.Count() > 0)
                    {
                        change.BillCount += queryChange.First().BillCount;
                        change.DayCount += queryChange.First().DayCount;
                        change.Free += queryChange.First().Free;
                        change.LoseCount += queryChange.First().LoseCount;
                        change.GroupId = groupId;
                        change.Profit += queryChange.First().Profit;
                        change.SumLost += queryChange.First().SumLost;
                        change.SumWin += queryChange.First().SumWin;
                        change.TradeHistoryId = tradeHistoryId;
                        change.UsedMargin += queryChange.First().UsedMargin;
                        change.WinCount += queryChange.First().WinCount;

                        change.AvgBillBenefit += queryChange.First().AvgBillBenefit;
                        change.AvgPostionTime += queryChange.First().AvgPostionTime;

                        change.OverStopLoss += queryChange.First().OverStopLoss;
                        change.PingJinPing += queryChange.First().PingJinPing;
                        change.RaiseRate += queryChange.First().RaiseRate;
                        change.RiskCount += queryChange.First().RiskCount;
                        change.Utilization += queryChange.First().Utilization;
                        change.WinRate += queryChange.First().WinRate;
                        change.Yield += queryChange.First().Yield;

                    }

                }
                change.BillCount = Math.Round(change.BillCount / operators.Count(), 2);
                change.DayCount = Math.Round(change.DayCount / operators.Count(), 2);
                change.Free = Math.Round(change.Free / operators.Count(), 2);
                change.LoseCount = Convert.ToInt32(change.LoseCount / operators.Count());
                change.Profit = Math.Round(change.Profit / operators.Count(), 2);
                change.SumLost = Math.Round(change.SumLost / operators.Count(), 2);
                change.SumWin = Math.Round(change.SumWin / operators.Count(), 2);
                change.UsedMargin = Math.Round(change.UsedMargin / operators.Count(), 2);
                change.WinCount = Convert.ToInt32(change.WinCount / operators.Count());
                change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / operators.Count(), 2);
                change.AvgPostionTime = Math.Round(change.AvgPostionTime / operators.Count(), 2);

                change.OverStopLoss = Math.Round(change.OverStopLoss / operators.Count(), 2);
                change.PingJinPing = Convert.ToInt32(change.PingJinPing / operators.Count());
                change.RaiseRate = Math.Round(change.RaiseRate / operators.Count(), 2);
                change.RiskCount = Convert.ToInt32(change.RiskCount / operators.Count());
                change.Utilization = Math.Round(change.Utilization / operators.Count(), 2);
                change.WinRate = Math.Round(change.WinRate / operators.Count(), 2);
                change.Yield = Math.Round(change.Yield / operators.Count(), 2);


            }

            db.GroupFiveDayAvg.Add(change);


        }
    }
}
